package cloudspace.vm.javassist.tests;

import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.zkoss.zk.ui.Executions;

import javassist.CannotCompileException;
import javassist.NotFoundException;
import junit.framework.TestCase;
import cloudspace.vm.VM;
import cloudspace.vm.javassist.MalformedConfigurationException;

public class JavassistTestCase extends TestCase
{
	public String realPath = "/home/mjw87/workspace/cloudspace/src";
	//public ByteCodeRuntime bct;
	public static VM virtualM;
	public void setUp()
	{
		
		virtualM = VM.currentVM(realPath);
		virtualM.registerThread();

	}
	public Object runMethod(String classToLoad,
			String methodToTest, Class<?>[] methodToTestArgs, Object argsToTest) throws InvocationTargetException
	{
		Class<?> rewrittenClass = loadClass(classToLoad);
		Method methodToRun = getClassMethod(rewrittenClass, methodToTest,
				methodToTestArgs);
		Object objectReturned = invokeMethod(methodToRun, argsToTest);
		return objectReturned;
	}
	private static Object invokeMethod(Method methodToRun, Object args) throws InvocationTargetException
	{
		Object createdObject = null;
		try
		{
			createdObject = (Object) methodToRun.invoke(null, (Object[]) args);
		}
		catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return createdObject;
	}
	@SuppressWarnings("unchecked")
	private static Method getClassMethod(Class rewrittenClass, String methodName,
			Class<?>[] args)
	{
		Method methodToRun = null;
		try
		{
			// Strange.....
			if (args == null)
			{		
				methodToRun = rewrittenClass
						.getDeclaredMethod(methodName, null);
			}
			else
			{
				methodToRun = rewrittenClass
						.getDeclaredMethod(methodName, args);
			}
		}
		catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return methodToRun;
	}
	private static Class loadClass( String classToLoad)
	{
		try
		{
			return (Class) virtualM.getLoader().loadClass(classToLoad);
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
